<template>
	<view class="material">
		<view class="menus dFlex jAround_aCenter">
			<view :class="['menuOne',type=='1'?'active':'']" @tap='changeMenu(1)'>
				<text>品牌动态</text>
				<view class="line"></view>
			</view>
			<view :class="['menuOne',type=='2'?'active':'']" @tap='changeMenu(2)'>
				<text>海报</text>
				<view class="line"></view>
			</view>
			<view :class="['menuOne',type=='3'?'active':'']" @tap='changeMenu(3)'>
				<text>活动</text>
				<view class="line"></view>
			</view>
			<view :class="['menuOne',type=='4'?'active':'']" @tap='changeMenu(4)'>
				<text>企业文化</text>
				<view class="line"></view>
			</view>
		</view>
		<view class="list" v-if='list.length'>
			<view class="line1 dFlex jStart_aStart" v-for="(item,index) in list" :key='index'>
				<view class="lineTx imgPublic">
					<image :src="item.logo"></image>
				</view>
				<view class="lineContent">
					<view class="userInfo dFlex jBetween_aCenter">
						<text>{{item.name}}</text>
						<view class="btns dFlex jEnd_aCenter">
							<view class="btn copy" @tap.stop='copy(index)'>复制文案</view>
							<!-- <view class="btn download" @tap.stop='downLoad(index)'>一键下载</view> -->
						</view>
					</view>
					<view class="time">{{item.create_time}}</view>
					<view class="desc">{{item.content}}</view>
					<view class="bigImg imgPublic" v-if="item.images.length == '1'" @tap='lookBig(0)'>
						<image :src="item.images[0].image_path" mode="widthFix"></image>
					</view>
					<view class="imgs dFlex jBetween_aCenter" v-else-if='item.images.length > 1'>
						<view class="bigImg imgPublic" @tap='lookBig(0)'>
							<image :src="item.images[0].image_path" mode="widthFix"></image>
						</view>
						<view class="smallImgs">
							<view class="smallOne imgPublic" v-if="item.images.length >= 2" @tap='lookBig(1)'>
								<image :src="item.images[1].image_path" mode="widthFix"></image>
							</view>
							<view class="smallOne imgPublic" v-if="item.images.length >= 3" @tap='lookBig(2)'>
								<image :src="item.images[2].image_path" mode="widthFix"></image>
							</view>
						</view>
					</view>
					<!-- <view class="shareIcon imgPublic">
						<image src="https://wx.gdxixiashi.com/public/images/person/fenxiang@2x.png" mode="widthFix"></image>
					</view> -->
				</view>
			</view>
		</view>
		<view class="list empty imgPublic" v-else>
			<image src="../static/images/empty.png" mode="widthFix"></image>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type:'1', //	1品牌动态2海报3活动4企业文化
				list:[],
			};
		},
		onLoad() {
			this.initData();
		},
		onShareAppMessage() {
			return{
				title:'洗匣士~您的家居窗帘清洗助手',
				path:'/pages/home?spreadId=' + (uni.getStorageSync('userId')?uni.getStorageSync('userId'):''),
				imageUrl:getApp().globalData.imgDomain+'public/logo.png',
				desc:'手机下单 | 专员取送 | 生态环保 | 轻奢洗护',
			}
		},
		onShareTimeline() {
			return{
				title:'洗匣士~您的家居窗帘清洗助手',
				path:'/pages/home?spreadId=' + (uni.getStorageSync('userId')?uni.getStorageSync('userId'):''),
				imageUrl:getApp().globalData.imgDomain+'public/logo.png',
				desc:'手机下单 | 专员取送 | 生态环保 | 轻奢洗护',
			}
		},
		methods:{
			initData(){
				this.tool.getData('Index/materialList',{
					type:this.type
				}).then(res=>{
					console.log(res);
					if(res){
						this.list = res.list;
					}
				}).catch(err=>{
					console.log(err);
				})
			},
			// 切换分类
			changeMenu(idx){
				this.type = idx;
				this.initData();
			},
			// 复制文案
			copy(index){
				let line = this.list[index];
				uni.setClipboardData({
					data:line.content,
				})
			},
			// 查看大图
			lookBig(idx){
				let line = this.list[idx];
				let arr = [];
				line.images.map((item,index)=>{
					arr.push(item.image_path);
				})
				let current = idx >= 0 && idx != ''?arr[idx]:arr[0];
				uni.previewImage({
					current:current,
					urls:arr,
				})
			},
			// 一键下载
			downLoad(index){
				let line = this.list[index];
				if(line.images.length){
					let count = 0;
					line.images.map((item,index)=>{
						uni.downloadFile({
							url:item.image_path,
							success: (thumb) => {
								if(thumb.errMsg == 'downloadFile:ok'){
									uni.saveImageToPhotosAlbum({
										filePath:thumb.tempFilePath,
										success: (res) => {
											count ++;
										},
										complete: () => {
											if(index == line.images.length-1){
												uni.showToast({
													title:'成功下载'+count+'张图片',
													icon:"success"
												})
											} 
										}
									})
								}
							},
							complete: () => {
								
							}
						})
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: white;
	}
	.material{
		width: 100%;
		.list{
			width: 100%;
			padding: 12px 4%;
			box-sizing: border-box;
			.line1{
				width: 100%;
				margin-bottom: 15px;
				padding-bottom: 12px;
				border-bottom: 1px solid #EEEEEE;
				.lineContent{
					flex:1;
					.shareIcon{
						width: 14px;
					}
					.time{
						font-size: 12px;
						color: #999999;
						margin: 6px 0 12px;
					}
					.bigImg{
						width: 156px;
						margin: 12px 0;
					}
					.imgs{
						width: 100%;
						margin-bottom: 13px;
						.bigImg{
							width: 204px;
							height: 120px;
							margin-right: 6px;
							border-radius: 4px;
							overflow: hidden;
							image{
								height:100%;
							}
						}
						.smallImgs{
							flex: 1;
							height: 120px;
							.smallOne{
								width: 100%;
								height: 57px;
								border-radius: 4px;
								overflow: hidden;
								margin-bottom: 6px;
							}
							.smallOne:last-child{
								margin: 0;
							}
						}
					}
					.userInfo{
						width: 100%;
						font-weight: bold;
						.btns{
							flex: 1;
							margin-left: 12px;
							.btn{
								width: max-content;
								font-weight: 400;
								padding: 4px 6px;
								border-radius: 50px;
								border: 1px solid transparent;
								margin-left: 9px;
								font-size: 12px;
							}
							.copy{
								color: #1664FF;
								border-color: #1664FF;
							}
							.download{
								background:#1664FF;
								color: white;
							}
						}
					}
				}
				.lineTx{
					width: 55px;
					height: 55px;
					overflow: hidden;
					margin-right: 10px;
					border-radius: 50%;
					image{
						height: 100%;
					}
				}
			}
			.line1:last-child{
				border: none;
			}
		}
		.menus{
			width: 100%;
			padding: 8px 0 0;
			.menuOne{
				flex: 1;
				text-align: center;
				font-weight: bold;
				color: #999999;
				.line{
					width: 14px;
					height: 2px;
					border-radius: 50px;
					margin: 7px auto 0;
					background: none;
				}
			}
			.active{
				color: #333333;
				.line{
					background: #1664FF;
				}
			}
		}
	}
</style>
